/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaymentView.java
 *
 * Created on Apr 9, 2010, 9:20:20 PM
 */
package ticketmaster.presentation.customer.purchase;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import ticketmaster.businesslogic.PaymentController;
import ticketmaster.businesslogic.PremiumCustomerController;
import ticketmaster.businessobject.CreditCardType;
import ticketmaster.businessobject.PurchaseType;
import ticketmaster.common.CardType;
import ticketmaster.common.PurchaseTypes;
import ticketmaster.dataobject.Bankcard;
import ticketmaster.dataobject.Order;
import ticketmaster.dataobject.PremiumCustomer;
import ticketmaster.presentation.customer.CustomerBasePanel;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Wei Liu
 */
public class PaymentView extends CustomerBasePanel
{

    private Order order;
    private PremiumCustomer premiumCustomer;

    /** Creates new form PaymentView */
    public PaymentView()
    {
        initComponents();
        initComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblCardType = new javax.swing.JLabel();
        cbCardType = new javax.swing.JComboBox();
        lblCardNumber = new javax.swing.JLabel();
        lblExpiryDate = new javax.swing.JLabel();
        tfCardNumber = new javax.swing.JTextField();
        cbExpiryMonth = new javax.swing.JComboBox();
        cbExpiryYear = new javax.swing.JComboBox();
        lblCardholder = new javax.swing.JLabel();
        tfCardholder = new javax.swing.JTextField();
        btnPurchase = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(PaymentView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, 330, 30));

        jLabel2.setBackground(resourceMap.getColor("jLabel2.background")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, 270, -1));

        lblCardType.setFont(resourceMap.getFont("lblCardType.font")); // NOI18N
        lblCardType.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblCardType.setText(resourceMap.getString("lblCardType.text")); // NOI18N
        lblCardType.setName("lblCardType"); // NOI18N
        add(lblCardType, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 140, 130, 40));

        cbCardType.setFont(resourceMap.getFont("cbExpiryMonth.font")); // NOI18N
        cbCardType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbCardType.setName("cbCardType"); // NOI18N
        add(cbCardType, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 140, 290, 40));

        lblCardNumber.setFont(resourceMap.getFont("lblCardNumber.font")); // NOI18N
        lblCardNumber.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblCardNumber.setText(resourceMap.getString("lblCardNumber.text")); // NOI18N
        lblCardNumber.setName("lblCardNumber"); // NOI18N
        add(lblCardNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 230, 130, -1));

        lblExpiryDate.setFont(resourceMap.getFont("lblExpiryDate.font")); // NOI18N
        lblExpiryDate.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblExpiryDate.setText(resourceMap.getString("lblExpiryDate.text")); // NOI18N
        lblExpiryDate.setName("lblExpiryDate"); // NOI18N
        add(lblExpiryDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, 110, -1));

        tfCardNumber.setFont(resourceMap.getFont("cbExpiryMonth.font")); // NOI18N
        tfCardNumber.setText(resourceMap.getString("tfCardNumber.text")); // NOI18N
        tfCardNumber.setName("tfCardNumber"); // NOI18N
        add(tfCardNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 220, 290, 40));

        cbExpiryMonth.setFont(resourceMap.getFont("cbExpiryMonth.font")); // NOI18N
        cbExpiryMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbExpiryMonth.setName("cbExpiryMonth"); // NOI18N
        add(cbExpiryMonth, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 300, 80, 40));

        cbExpiryYear.setFont(resourceMap.getFont("cbExpiryMonth.font")); // NOI18N
        cbExpiryYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbExpiryYear.setName("cbExpiryYear"); // NOI18N
        add(cbExpiryYear, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 300, 80, 40));

        lblCardholder.setFont(resourceMap.getFont("lblCardholder.font")); // NOI18N
        lblCardholder.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblCardholder.setText(resourceMap.getString("lblCardholder.text")); // NOI18N
        lblCardholder.setName("lblCardholder"); // NOI18N
        add(lblCardholder, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 380, 160, 40));

        tfCardholder.setFont(resourceMap.getFont("cbExpiryMonth.font")); // NOI18N
        tfCardholder.setText(resourceMap.getString("tfCardholder.text")); // NOI18N
        tfCardholder.setName("tfCardholder"); // NOI18N
        add(tfCardholder, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 380, 290, 40));

        btnPurchase.setFont(resourceMap.getFont("btnPurchase.font")); // NOI18N
        btnPurchase.setText(resourceMap.getString("btnPurchase.text")); // NOI18N
        btnPurchase.setName("btnPurchase"); // NOI18N
        btnPurchase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPurchaseActionPerformed(evt);
            }
        });
        add(btnPurchase, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 470, 290, 50));
    }// </editor-fold>//GEN-END:initComponents

    private void btnPurchaseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPurchaseActionPerformed
    {//GEN-HEADEREND:event_btnPurchaseActionPerformed
        PaymentController controller = new PaymentController();
        Bankcard card = new Bankcard();
        CreditCardType cardType = (CreditCardType) this.cbCardType.getSelectedItem();
        card.setCardNumber(this.tfCardNumber.getText());
        card.setCardType(cardType.getType().ordinal());
        card.setCardholderName(this.tfCardholder.getText());
        card.setExpiryYear(Integer.parseInt(this.cbExpiryYear.getSelectedItem().toString()));
        card.setExpiryMonth(Integer.parseInt(this.cbExpiryMonth.getSelectedItem().toString()));

        Calendar orderDate = new GregorianCalendar();
        orderDate.setTimeInMillis(order.getOrderDate().getTime());
        orderDate.add(Calendar.MINUTE, 10);

        if (orderDate.before(DateTimeHelper.getCurrentDateTimeCalendar()))
        {
            JOptionPane.showMessageDialog(this, "Session expired. Please book your tickets again.");

            SearchView sv = new SearchView();
            sv.setPnlParent(this.getPnlParent());
            this.getPnlParent().removeAll();
            this.getPnlParent().add(sv);
            this.getPnlParent().validate();
            this.getPnlParent().repaint();

            return;
        }

        try
        {
            controller.payFee(order, card, this.getBookedTickets());
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            return;
        }

        if (premiumCustomer != null && PurchaseType.getPurchaseType() != PurchaseTypes.Continue)
        {
            PremiumCustomerController premiumCustomerController = new PremiumCustomerController();
            premiumCustomerController.updatePremiumCustomer(premiumCustomer);
        }

        PaymentConfirmationView pcv = new PaymentConfirmationView(order, card);
        //pcv.setBankcard(card);
        //pcv.setOrder(order);
        pcv.setPnlParent(this.getPnlParent());
        if (this.getMainFrame() != null)
        {
            pcv.setMainFrame(this.getMainFrame());
        }
        pcv.setPnlParent(this.getPnlParent());
        this.getPnlParent().removeAll();
        this.getPnlParent().add(pcv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
        //JOptionPane.showMessageDialog(this, "success!");
    }//GEN-LAST:event_btnPurchaseActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPurchase;
    private javax.swing.JComboBox cbCardType;
    private javax.swing.JComboBox cbExpiryMonth;
    private javax.swing.JComboBox cbExpiryYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblCardNumber;
    private javax.swing.JLabel lblCardType;
    private javax.swing.JLabel lblCardholder;
    private javax.swing.JLabel lblExpiryDate;
    private javax.swing.JTextField tfCardNumber;
    private javax.swing.JTextField tfCardholder;
    // End of variables declaration//GEN-END:variables

    private void initComboBox()
    {
        CreditCardType cardType1 = new CreditCardType(CardType.Visa, "Visa");
        CreditCardType cardType2 = new CreditCardType(CardType.MasterCard, "MasterCard");
        CreditCardType cardType3 = new CreditCardType(CardType.AmericanExpress, "American Express");

        cbCardType.removeAllItems();
        cbCardType.addItem(cardType1);
        cbCardType.addItem(cardType2);
        cbCardType.addItem(cardType3);

        cbExpiryMonth.removeAllItems();
        for (int i = 1; i <= 12; i++)
        {
            cbExpiryMonth.addItem(i);
        }

        cbExpiryYear.removeAllItems();
        cbExpiryYear.addItem(2010);
        cbExpiryYear.addItem(2011);
        cbExpiryYear.addItem(2012);
        cbExpiryYear.addItem(2013);
    }

    private static void createAndShowGUI()
    {

        //Create and set up the window.
        JFrame frame = new JFrame("demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        PaymentView newContentPane = new PaymentView();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    /**
     * @return the order
     */
    public Order getOrder()
    {
        return order;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order)
    {
        this.order = order;
    }

    /**
     * @return the premiumCustomer
     */
    public PremiumCustomer getPremiumCustomer()
    {
        return premiumCustomer;
    }

    /**
     * @param premiumCustomer the premiumCustomer to set
     */
    public void setPremiumCustomer(PremiumCustomer premiumCustomer)
    {
        this.premiumCustomer = premiumCustomer;
    }
}
